package com.heitian.ssm.controller;

import com.google.gson.JsonObject;
import com.heitian.ssm.model.Linkman;
import com.heitian.ssm.model.PageBean;
import com.heitian.ssm.model.User;
import com.heitian.ssm.service.UserService;
import com.heitian.ssm.utils.CommonUtils;
import com.heitian.ssm.utils.GsonUtil;
import com.heitian.ssm.utils.RegexUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 作者： 陈建峰
 * 创建时间： 17/10/16 下午4:13
 * 版本： [1.0, 17/10/16]
 * 描述： 用户处理
 */
@Controller
@RequestMapping("/user")
public class UserController {

    private Logger log = Logger.getLogger(UserController.class);

    @Resource
    private UserService userService;

    @Value("#{settings['AdminPhoneNum']}")
    private String AdminPhoneNum = "";

    /**
     * 用户登录
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/register",method=RequestMethod.POST)
    public void register(HttpServletRequest request,HttpServletResponse response) throws IOException{

        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        User user = new User();
        if (paras.has("userGuid")) {
            user.setUserGuid(paras.get("userGuid").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("userName")) {
            user.setUserName(paras.get("userName").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("photoUrl")) {
            user.setPhotoUrl(paras.get("photoUrl").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("sex")) {
            user.setSex(paras.get("sex").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("adress")) {
            user.setAdress(paras.get("adress").getAsString());
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        if (paras.has("telephoneNum")) {
            user.setTelephoneNum(paras.get("telephoneNum").getAsString());
            if(AdminPhoneNum.contains(paras.get("telephoneNum").getAsString())){
                user.setIsAdmin("1");
            }else{
                user.setIsAdmin("0");
            }
        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
            return;
        }
        //将用户名和密码传递给service层
        if(userService.getUserByUserGuid(user.getUserGuid())!=null){
            response.getWriter().write(CommonUtils.createJson("0", "用户已存在", null).toString());
            return;
        }else{
            userService.register(user);
        }
        String UserArea = null;
        String code = "1";
        UserArea = GsonUtil.GsonString(user);
        response.getWriter().write(CommonUtils.createJson(code, "保存用户信息成功！", UserArea).toString());
    }

    /**
     * 获取用户
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/getUserByUserGuid",method=RequestMethod.POST)
    public void getUserByUserGuid(HttpServletRequest request,HttpServletResponse response) throws IOException {

        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        if (paras.has("userGuid")) {
            User user = userService.getUserByUserGuid(paras.get("userGuid").getAsString());
            String UserArea = null;
            String code = "0";
            if(user!=null){
                code = "1";
                UserArea = GsonUtil.GsonString(user);
                response.getWriter().write(CommonUtils.createJson(code, "", UserArea).toString());
            }else{
                response.getWriter().write(CommonUtils.createJson(code, "用户不存在", UserArea).toString());
            }

        } else {
            response.getWriter().write(CommonUtils.createJson("0", "参数不合法", null).toString());
        }
    }

    /**
     * 获取用户
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/getAllUser", method = RequestMethod.POST)
    public void getAllUser(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String userName = paras.get("userName").getAsString();
        String index = paras.get("index").getAsString();
        String currentPageCount = paras.get("currentPageCount").getAsString();
        PageBean<User> pageBean = userService.getAllUser(userName,Integer.parseInt(index),Integer.parseInt(currentPageCount));
        String UserArea = null;
        String code = "0";
        String message = "";
        if (pageBean != null) {
            code = "1";
            UserArea = GsonUtil.GsonString(pageBean);
        } else {
            message = "用户数据获取失败！";
        }
        response.getWriter().write(CommonUtils.createJson(code, message, UserArea).toString());
    }

    /**
     * 删除用户
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
    public void deleteUser(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        JsonObject paras = CommonUtils.getJson(request);
        if (paras == null) {
            response.getWriter().write(CommonUtils.createJson("0", "", null).toString());
            return;
        }
        String userGuids = paras.get("userGuids").getAsString();
        String[] userGuid = userGuids.split(";");
        for (int i = 0; i < userGuid.length; i++) {
            userService.deleteUser(userGuid[i]);
        }
        response.getWriter().write(CommonUtils.createJson("1", "删除成功！", null).toString());
    }
}
